package cn.credit.loan.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.CharUtils;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.credit.loan.entity.Check;
import cn.credit.loan.service.check.CheckService;
/**
 * 审查专员控制类
 * @author 
 *
 */
@Controller
public class CheckController {

		//日志输出
		private Logger logger = Logger.getLogger(CheckController.class);
		@Autowired
		private CheckService checkService;
		
		/**
		 * 审查专员登录
		 * @param checkName
		 * @param checkPwd
		 * @param model
		 * @return
		 */
		@RequestMapping(value="/checkdologin")
		public ModelAndView login(@RequestParam("adminName")String checkName,
						    @RequestParam("adminPwd")String checkPwd,Model model,HttpSession session){
				try {
					logger.info("####---------->> 进入审查员处理，对页面传过来的值进行传值到后台");
					Check checks=checkService.checklogin(checkName, checkPwd);
					if(checks!=null){
						if(!checks.getCheckPwd().equals(checkPwd)){
							session.setAttribute("errorpwd", "密码输入不正确");
							model.addAttribute("errorpwd", "密码输入不正确");
							return new ModelAndView("after/login","roleModel",model);
						}
						session.setAttribute("checks", checks);
						session.setAttribute("role", 2);
						session.setAttribute("rolelogin", "审查专员");
						return new ModelAndView("after/index","roleModel",session);
					}else{
						model.addAttribute("errorName", "审查专员不存在");
					}
				} catch (Exception e) {
					model.addAttribute("syserror","出现错误，请联系管理员");
					logger.info("####------->> error =" +"出现错误，请联系管理员");
					e.printStackTrace();
				}
				return new ModelAndView("after/login","roleModel",model);
		}
		
		/**
		 * 审查专员查询
		 * @param check
		 * @param model
		 * @return
		 */
		@RequestMapping(value="/checklist")
		@ResponseBody
		public Object getCheckList(Check check,Model model)
		{
			try {
				List<Check> checks=checkService.getCheckList(check);
				if(checks!=null)
				{
					model.addAttribute("check", check);
					return checks;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		/**
		 * 审查专员添加
		 * @param check
		 * @param model
		 * @return
		 */
		@RequestMapping("/checkadd")
		public String getCheckAdd(Check check,Model model)
		{
			try {
				check.setCheckName("测试001");
				check.setCheckPwd("123456");
				boolean checks=checkService.getCheckAdd(check);
				if(checks)
				{
					model.addAttribute("check", check);
					return "CheckAdd";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
			
		}
		
		/**
		 * 审查专员更新
		 * @param check
		 * @param model
		 * @return
		 */
		@RequestMapping("/checkupdate")
		public String getCheckUpdate(Check check,Model model)
		{
			try {
				check.setCheckName("测试001");
				check.setCheckPwd("123456");
				check.setCheckSex(CharUtils.toCharacterObject("男"));
				boolean checks=checkService.getCheckUpdate(check);
				if(checks){
					model.addAttribute("check", check);
					return "CheckUpdate";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
			
		}
		
		/**
		 * 审查专员删除
		 * @param checkId
		 * @param model
		 * @return
		 */
		@RequestMapping("/checkdelete")
		public String getCheckAdd(int checkId,Model model)
		{
			try {
		
				boolean checks=checkService.getCheckDelete(checkId);
				if(checks)
				{
					model.addAttribute("checkId", checkId);
					return "CheckDelete";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
			
		}
		
		
}
